package com.czn.controller.admin;

import com.czn.entity.User;
import com.czn.service.IUserService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


/**
 * 管理员控制器
 *
 * @author ChenZeNan
 * @create 2021-04-15 19:00
 */
@Controller
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    private IUserService userService;

    /**
     * 跳转到管理员管理页面
     *
     * @return
     */
    @RequestMapping("/manage")
    public String doctorManage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                               @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                               @RequestParam(required = false) String username,
                               Model model) {
        PageQuery<User> page = userService.findUserList(pageNo, pageSize, username);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("username", username);
        model.addAttribute("path", "/admin/admin/manage");
        model.addAttribute("params", "");
        if (username != null) {
            model.addAttribute("params", "&username=" + username);
        }
        return "admin/adminManage";
    }

    /**
     * 跳转到新增管理员页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("admin", new User());
        return "admin/adminForm";
    }

    /**
     * 跳转到管理员信息修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        model.addAttribute("admin", userService.findUserById(id));
        return "admin/adminForm";
    }


    /**
     * 新增管理员
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody User user) {
        ResponseResult result = userService.addUser(user);
        return result;
    }

    /**
     * 编辑管理员
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody User user) {
        ResponseResult result = userService.editUser(user);
        return result;
    }

    /**
     * 删除管理员
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = userService.deleteUserById(id);
        return result;
    }

}
